public class Solution {
    public int majorityElement(int[] nums) {
        int tmp = 0;
        int count = 0;
        for(int i = 0; i < nums.length ; i++) {
            if(tmp != nums[i] && count == 0) {
                tmp = nums[i];
            }
            if(tmp != nums[i]) {
                count--;
            }
            if(tmp == nums[i]) {
                count++;
            }
        }
        return tmp;
    }
    public void rotate(int[] nums, int k) {
        int[] arr = new int[k];
        int count = 0;
        for(int i = nums.length - k; i < nums.length ; i ++) {
            arr[count++] = nums[i];
        }
        for(int i = nums.length-1-k; i >= 0; i --) {
            nums[i + k] = nums[i];
        }
        for(int i = 0; i < count;i ++) {
            nums[i] = arr[i];
        }
    }
    public int maxProfit(int prices[]) {
        int minprice = Integer.MAX_VALUE;
        int maxprofit = 0;
        for (int i = 0; i < prices.length; i++) {
            if (prices[i] < minprice) {
                minprice = prices[i];
            } else if (prices[i] - minprice > maxprofit) {
                maxprofit = prices[i] - minprice;
            }
        }
        return maxprofit;
    }
    public int maxProfit1(int[] prices) {
        int max = prices[0];
        int min = prices[0];
        int len = prices.length;
        int tmp = 0;
        for(int i = 0; i < len - 1 ; i ++) {
            if (prices[i] >= max) {
                max = prices[i];
            }
            if(prices[i] <= min) {
                min = prices[i];
                max = prices[i];
            }
            if(prices[i] > prices[i + 1]) {
                tmp = max -min;
                tmp+=tmp;
            }
        }
        return tmp;

    }
}
